package com.michaellou.tools.multidb;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUtil
{
	private static final String ENCODING="UTF-8";
	
	public static boolean createFolder(String folderPath) throws IOException
	{
		boolean result = false;
		File f = new File(folderPath);
		result = f.mkdirs();
		return result;
	}

	public void emptyDirectory(File directory)
	{
		File[] entries = directory.listFiles();
		for (int i = 0; i < entries.length; i++)
		{
			entries[i].delete();
		}
	}

	public static boolean makeFile(String filepath) throws IOException
	{
		boolean result = false;
		File file = new File(filepath);
		result = file.createNewFile();
		file = null;
		return result;
	}

	public static boolean isDel(String filepath)
	{
		boolean result = false;
		File file = new File(filepath);
		result = file.delete();
		file = null;
		return result;
	}

	public static boolean renamefile(String filepath, String destname)
	{
		boolean result = false;
		File f = new File(filepath);
		String fileParent = f.getParent();
		String filename = f.getName();
		File rf = new File(fileParent + "//" + destname);
		if (f.renameTo(rf))
		{
			result = true;
		}
		f = null;
		rf = null;
		return result;
	}

	public static void WriteFile(String filepath, String content)
			throws Exception
	{
		FileWriter filewriter = new FileWriter(filepath, true);
		PrintWriter printwriter = new PrintWriter(filewriter);
		printwriter.println(content);
		printwriter.flush();
		printwriter.close();
		filewriter.close();
	}

	public static void logBak(String filePath, long baksize) throws IOException
	{
		File f = new File(filePath);
		long len = f.length();
		SimpleDateFormat simpledateformat = new SimpleDateFormat(
				"yyyyMMddHHmmss");
		String s = simpledateformat.format(new Date());
		String fileName = f.getName();
		int dot = fileName.indexOf(".");
		String bakName = s + fileName.substring(dot);
		System.out.println(bakName);
		if (len >= baksize)
		{
			renamefile(filePath, bakName);
			makeFile(filePath);
		}
		f = null;
	}
	
	public static void mergeFile(String dir,String suffix,String dstFile){
		File dirF = new File(dir); 
		File dstF=new File(dstFile);
        if (dirF.isDirectory()) { 
            File[] list = dirF.listFiles(); 
            OutputStream out=null;
            try {
	            out = new FileOutputStream(dstF); 
	            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out, ENCODING)); 
	            for (int i = 0; i < list.length; i++) { 
	                File file = list[i]; 
	                //过滤掉后缀名不符的文件
	                if(null==suffix || "".equals(suffix) || file.getName().toLowerCase().endsWith(suffix)){
	                	InputStream in = null;
	                		try{
								in = new FileInputStream(file);
								BufferedReader br = new BufferedReader(new InputStreamReader(in, ENCODING)); 
								
								System.out.println("----------------read file:["+file.getName()+"]-----------------");
								String line = null; 
						        while ((line = br.readLine()) != null) { 
						            bw.write(line+"\r\n"); 
						        } 
								
	                		}catch (FileNotFoundException e) {
	        	            	e.printStackTrace();
	        	            }catch (UnsupportedEncodingException e) {
	        	            	e.printStackTrace();
	        	            }catch (IOException e) {
								e.printStackTrace();
							} finally{
									try {
										if(null!=in){
											in.close();
										}
									} catch (IOException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
	        	            }
	                }
	            } 
            } catch (FileNotFoundException e) {
            	e.printStackTrace();
            }catch (UnsupportedEncodingException e) {
            	e.printStackTrace();
            }finally{
				try {
					if(null!=out){
						out.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            } 
        }
	}
	
	public static void mergeFileWithSubstr(String dir,String substr,String dstFile){
		File dirF = new File(dir); 
		File dstF=new File(dstFile);
        if (dirF.isDirectory()) { 
            File[] list = dirF.listFiles(); 
            OutputStream out=null;
            try {
	            out = new FileOutputStream(dstF); 
	            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out, ENCODING)); 
	            for (int i = 0; i < list.length; i++) { 
	                File file = list[i]; 
	                //过滤掉后缀名不符的文件
	                if(null==substr || "".equals(substr) || file.getName().toLowerCase().contains(substr)){
	                	InputStream in = null;
	                		try{
								in = new FileInputStream(file);
								BufferedReader br = new BufferedReader(new InputStreamReader(in, ENCODING)); 
								
								System.out.println("----------------read file:["+file.getName()+"]-----------------");
								String line = null; 
						        while ((line = br.readLine()) != null) { 
						            bw.write(line+"\r\n"); 
						        } 
								
	                		}catch (FileNotFoundException e) {
	        	            	e.printStackTrace();
	        	            }catch (UnsupportedEncodingException e) {
	        	            	e.printStackTrace();
	        	            }catch (IOException e) {
								e.printStackTrace();
							} finally{
									try {
										if(null!=in){
											in.close();
										}
									} catch (IOException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
	        	            }
	                }
	            } 
            } catch (FileNotFoundException e) {
            	e.printStackTrace();
            }catch (UnsupportedEncodingException e) {
            	e.printStackTrace();
            }finally{
				try {
					if(null!=out){
						out.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            } 
        }
	}
	public static void main(String[] args) {
		mergeFile(args[0],".txt",args[0]+"/dw_renshu_count.txt");
	}
}